
package org.winexpress

import org.codehaus.groovy.grails.web.json.JSONObject;
import org.codehaus.groovy.grails.web.json.JSONArray;
import grails.converters.JSON

/**
 *
 * @author Swee Zhi
 */
class ProductManagement {
    
    def listProducts() {
        def products = Product.list()
        JSONArray arr = new JSONArray()
        products.each() {
            JSONObject obj = new JSONObject()
            obj.put("id",it.id)
            obj.put("productName",it.productName)
            obj.put("description",it.description)
            obj.put("imageUrl",it.imageUrl)
            obj.put("productType",it.productType)
            obj.put("size",it.size)
            obj.put("costPrice",it.costPrice)
            obj.put("sellingPrice",it.sellingPrice)
            obj.put("stockCount",it.stockCount)
            arr.add(obj)
        }
        
        return arr
    }
    
    def getProductByID(id) {
        def product = Product.findById(id)
        return product
    }
    
//    def getProductByName(productName) {
//        def Product = Product.findByproductName(productName)
//        JSONObject obj = new JSONObject()
//        obj.put("id",it.id)
//        obj.put("productName",it.productName)
//        obj.put("description",it.description)
//        obj.put("imageUrl",product.imageUrl)
//        
//        return obj
//    }

    def getProductForDbByProductID(id) {
        def c = Product.findById(id)
        
        return c
    }

    def searchProducts(productType, optionsList) {
        ArrayList<ProductOption> options = products.productOptions.toList()
        
        def searchResults = Product.withCriteria {
            eq("productType",productType)            
//            and{
//                eq("pest",pest)               
//                eq("stage","4")               
//            }
        }   
        
        println searchResults
    }
    
    def addProduct(o) {
        def status
        
        Product p = new Product()
        p.productName = o.get("productName")
        if (o.get("description") == "") {
            p.description = "-"
        }
        else {
            p.description = o.get("description")
        }
//        p.imageUrl = o.get("imageUrl")
//        if (o.get("imageUrl") == "") {
//            wt.description = "-"
//        }
//        else {
//            wt.description = o.get("description")
//        }
        p.imageUrl = "images/product/wine/noimage.jpg"

        ProductTypeManagement ptman = new ProductTypeManagement()
        def pt = ptman.getProductTypeForDbByProductTypeName(o.get("productType"))
        p.productType = pt
        
        // all the names retrieved from select are string
        if (o.has("wineType")) {
            ProductOption po = new ProductOption()
            po.optionType = "wineType"
            WineTypeManagement wtman = new WineTypeManagement()
            def wt = wtman.getWineTypeForDbByWineTypeName(o.get("wineType"))
            po.optionTypeId = wt.id
            p.addToProductOptions(po)
        }
        if (o.has("wineVarietal")) {
            ProductOption po = new ProductOption()
            po.optionType = "wineVarietal"
            WineVarietalManagement wvman = new WineVarietalManagement()
            def wv = wvman.getWineVarietalForDbByWineVarietalName(o.get("wineVarietal"))
            po.optionTypeId = wv.id
            p.addToProductOptions(po)
        }
        if (o.has("wineRegion")) {
            ProductOption po = new ProductOption()
            po.optionType = "wineRegion"
            WineRegionManagement wrman = new WineRegionManagement()
            def wr = wrman.getWineRegionForDbByWineRegionName(o.get("wineRegion"))
            po.optionTypeId = wr.id
            p.addToProductOptions(po)
        }
        
        p.size = o.get("size")
        p.costPrice = o.get("costPrice")
        p.sellingPrice = o.get("sellingPrice")
        p.stockCount = o.get("stockCount")
        
        if (p.save(flush:true)) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Add Wine >> ' + status
        
        return status
    }
    
    def updateProduct(product, updatedOptionList) {
        def status
        
        println "updateProduct IN OK "
        
        ProductOptionManagement poman = new ProductOptionManagement()
        
        Product p = Product.findById(product.id)
        if (product.has("productName")) { p.productName = product.productName }
        if (product.has("description")) { p.description = product.description }
//        if (product.has("imageUrl")) { p.imageUrl = product.imageUrl }
p.imageUrl = "images/product/wine/noimage.jpg"
        if (product.has("size")) { p.size = product.size }
        if (product.has("costPrice")) { p.costPrice = product.costPrice }
        if (product.has("sellingPrice")) { p.sellingPrice = product.sellingPrice }
        if (product.has("stockCount")) { p.stockCount = product.stockCount }

        def productOptionsUpdateStatus = false
        updatedOptionList.each() {
            productOptionsUpdateStatus = poman.updateProductOption(it)
        }
        
        if (productOptionsUpdateStatus == true) {
            if (p.save(flush:true)) {
                status = true
            }
            else {
                status = false
            }        
            println 'Management >> Update Wine >> ' + status
        }
        else {
            status = false
        }
        
        return status
    }
    
    def deleteProduct(id) {
        def status
        
        Product p = Product.findById(id)
        if (p.delete()) {
            status = true
        }
        else {
            status = false
        }        
        println 'Management >> Delete Wine >> ' + status
        
        return status
    }
	
}

